package com.sakura.probe.controller;

import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.util.JwtTokenUtil;
import com.sakura.probe.nosql.redis.RedisKey;
import com.sakura.probe.nosql.redis.RedisService;
import com.sakura.probe.pojo.Comment;
import com.sakura.probe.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Api(tags = "CommentController",description = "话题评论")
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisService redisService;

    @ApiOperation("获取评论内容")
    @ResponseBody
    @GetMapping("/getCommentList")
    public Map<String,Object> getTopicComment(int pageIndex, int pageSize, Long topicId){
        HashMap<String,Object> modelMap = new HashMap<>();
        Comment condition = new Comment();
        condition.setTopicId(topicId);
        condition.setParentId(0L);
        //获取父评论
        List<Comment> list = commentService.listByCondition(condition, pageIndex, pageSize);
        modelMap.put("data",list);
        modelMap.put("count",commentService.countByCondition(condition));
        modelMap.put("success",true);
        return modelMap;
    }

    @ApiOperation("获取回复评论的列表")
    @ResponseBody
    @GetMapping("/getCommentChildList")
    public Map<String,Object> getTopicCommentChild(int pageIndex,int pageSize,Long commentId){
        Map<String,Object> modelMap = new HashMap<>();
        Comment condition = new Comment();
        condition.setParentId(commentId);
        modelMap.put("data",commentService.listByCondition(condition,commentId,pageIndex,pageSize));
        modelMap.put("count",commentService.countByCondition(condition));
        modelMap.put("success",true);
        return modelMap;
    }

    @ApiOperation("发表评论")
    @ResponseBody
    @PostMapping("/postComment")
    @RequiresRoles(logical = Logical.OR,value = {"0","1","2","3"})
    public CommonResult postComment(Long topicId, HttpServletRequest request,
                                    Long commentId, String content, boolean type){
        Long userId = Long.valueOf(JwtTokenUtil.getUsernameFromToken(request.getHeader("Token")));
        Comment condition;
        if (type){
            //对话题的评论
            condition = new Comment(topicId,userId,0L,content);
        }else {
            //回复某一个人的评论
            condition = new Comment(topicId,userId,commentId,content);
        }
        boolean result = commentService.save(condition);
        return result?CommonResult.success("发表成功"):CommonResult.failed("发表失败");
    }

}
